<?php
include_once( "include/global.php" );
include_once( "include/process_db.php" );
include_once( "include/check_login.php" );

header('Cache-Control: no-cache, must-revalidate');

// the data array to return
$ret = array();
$ret["status"] = "success";
$ret["error_msg"] = "";

// open database
OpenDB();

// get all within the range
$condition = "Date>='" . $_GET["start_date"] . "' AND Date<='" . $_GET["end_date"]. "'";
if( $_GET["user_id"] != "" ) $condition .= " AND UserID=" . $_GET["user_id"];
$ret["accounting_consume_list"] = QueryRecordData( "main", $condition . " AND FieldID<>9999 ORDER BY DATE ASC" );
$ret["accounting_income_list"] = QueryRecordData( "main", $condition . " AND FieldID=9999 ORDER BY DATE ASC" );

// get total
$consume_total_condition = "Date<='" . $_GET["end_date"]. "' AND FieldID<>9999";
$income_total_condition = "Date<='" . $_GET["end_date"]. "' AND FieldID=9999";
if( $_GET["user_id"] != "" )
{
	$consume_total_condition .= " AND UserID=" . $_GET["user_id"];
	$income_total_condition .= " AND UserID=" . $_GET["user_id"];
}
$accounting_consume_total = QueryRecordData( "main", $consume_total_condition, "SUM(Amount) as AMOUNT_TOTAL" );
$accounting_income_total = QueryRecordData( "main", $income_total_condition, "SUM(Amount) as AMOUNT_TOTAL" );
$ret["accounting_total"] = $accounting_income_total[0]["AMOUNT_TOTAL"] - $accounting_consume_total[0]["AMOUNT_TOTAL"];

// close database
CloseDB();

// return data
echo json_encode( $ret );
?>
